<?php
class Comment_modul  extends CI_Model {

    public $rub = array();
    var $one = 1;
    public function __construct() {
        // Call the Model constructor
        parent::__construct();
    }

    public function comments($id = 0, $start = 0, $finish = 500) {
        // запрос списка новостей
        $com = false;
        $select = "SELECT *  FROM `comment` where   `id_news` = {$id} and `delete` = '0' and `activ`='1' ORDER BY  `date` ASC  limit {$start}, {$finish}";

        $q = $this -> db -> query($select);
        if ($q -> num_rows() > 0) {
            foreach ($q -> result() as $row) {
                $com[] = array('id' => $row -> id, 'name' => $row -> name, 'date' => $row -> date, 'time' => date("d.m.Y в H:i", $row -> date), 'text' => $row -> text, );
            }
        }

        return $com;

    }

    public function save($id = 0, $text = '', $email = '', $name = '') {

        # запись в базу

        $text = str_replace("\n", '<br>', $text);

        $data = array('date' => time(), 'id_news' => $id, 'email' => $email, 'activ' => 1, 'name' => str_replace(array("'", '"', '`', '\\'), '', $name), 'text' => $this -> db -> escape($text), 'ip' => '', );
        $this -> db -> insert('comment', $data);
        # Получаем уникальный ID
        $id_news = $this -> db -> insert_id();
        # генерация ссылок
        return $id_news;
    }

}
